package itsm.isperp.control.form.table;

import org.apache.commons.lang3.StringUtils;

/**
 * 
 * 由多行所组成的表格
 * 
 * @author lizx
 * @date 2013-11-8
 * @since 1.0
 * 
 */
public class TreeTable extends NormalTreeTable<TreenodeRow> {

	/**
	 * 
	 * @param treenodeNamePath
	 * @param code
	 * @param calculateFormula
	 * @param value
	 */
	public void addRow(String id, String treenodeNamePath, String code,
			String calculateFormula, String value) {

		// 1、构建行，并比较当前cell的upon是否名称一样，如果是则upon的rowspan+1;
		// 2、构造到最后一列比较列总数与maxColumn只差，colSpan=maxColumn-size;
		// calculateFormula = calculateFormula.replace("%", "/100");
		int rowsSize = rows.size();
		int i2 = treenodeNamePath.indexOf("/", 1);
		String[] nodes = treenodeNamePath.substring(i2 + 1).split("/");

		// 取得最后一行作为上一行
		TreenodeRow upRow = null;
		int upRowCellsLen = 0;
		if (rowsSize > 0) {
			upRow = this.rows.get(rowsSize - 1);
			upRowCellsLen = upRow.cells.size();
		}

		TreenodeRow row = new TreenodeRow();

		int nodesLen = nodes.length;

		if (upRowCellsLen < nodesLen && maxColumn < nodesLen) {
			maxColumn = nodesLen;
		}

		for (int i = 0; i < nodesLen; i++) {
			String name = nodes[i];
			setRow(row, id, name, i, upRowCellsLen, rowsSize, upRow,
					calculateFormula, code);
		}

		row.calculateFormula = calculateFormula;
		row.value = value;

		rows.add(row);

	}

	private void setRow(TreenodeRow row, String id, String name,
			int currentRowCellIndex, int upRowCellsLen, int rowsSize,
			TreenodeRow upRow, String calculateFormula, String code) {
		TreenodeCell newCell = new TreenodeCell();

		String[] names = name.split("┆");
		if (currentRowCellIndex < upRowCellsLen) {
			TreenodeCell upRowCell = upRow.cells.get(currentRowCellIndex);
			// 如果需要合并必然不是最后的叶子节点
			if (names[0].equals(upRowCell.name)) {
				if (upRowCell.rowSpan == 0) {
					upRowCell = findUpCell(upRowCell, rowsSize - 1,
							currentRowCellIndex);
				}

				upRowCell.addCalculateFormula(calculateFormula, code);
				upRowCell.rowSpan += 1;
				newCell.rowSpan = 0;
			} else {
				newCell.addCalculateFormula(calculateFormula, code);
			}
		} else {
			newCell.addCalculateFormula(calculateFormula, code);
		}

		newCell.name = names[0];
		newCell.code = names[1];
		newCell.id = id;

		row.addCell(newCell);
	}

	/**
	 * 
	 * @param upRowCell
	 * @param lastIndex
	 * @param cellIndex
	 * @return
	 */
	private TreenodeCell findUpCell(TreenodeCell upRowCell, int lastIndex,
			int cellIndex) {
		TreenodeRow row = this.rows.get(lastIndex);
		TreenodeCell cell = row.cells.get(cellIndex);
		if (cell.rowSpan > 0) {
			return cell;
		}
		return findUpCell(upRowCell, lastIndex - 1, cellIndex);
	}

	@Override
	public String renderHtml() {
		// 输出为html
		StringBuilder sb = new StringBuilder();
		sb.append("<tr>");
		sb.append("<th colSpan='").append(maxColumn).append("'>").append("科目")
				.append("</th>");

		sb.append("<th style='width:150px'>金额</th>");
		sb.append("<tr>");

		for (TreenodeRow row : this.rows) {

			int size = row.cells.size() - 1;

			sb.append("<tr>");

			for (int i = 0; i < size; i++) {
				TreenodeCell cell = row.cells.get(i);
				if (cell.rowSpan > 0) {
					sb.append("<td rowSpan='" + cell.rowSpan + "' data-code='"
							+ cell.code + "'>");
					sb.append(cell.name).append("(").append(cell.code)
							.append(")");

					String cal = "0";

					for (String str : cell.includeCalculateFormulas) {
						cal += "+" + str;
					}

					sb.append("<input style='display:none'  type='text'  id='cost_"
							+ cell.code
							+ "' class='iscalculate' data-code='"
							+ cell.code + "'  data-calculate='" + cal + "'>");
					sb.append("</td>");
				}
			}

			TreenodeCell cell = row.cells.get(size);

			if (size + 1 < this.maxColumn) {
				sb.append("<td rowSpan='" + cell.rowSpan + "' colSpan='"
						+ (this.maxColumn - size) + "' data-code='" + cell.code
						+ "'>");
			} else {
				sb.append("<td rowSpan='" + cell.rowSpan + "' data-code='"
						+ cell.code + "'>");
			}
			sb.append(cell.name).append("(").append(cell.code);

			// 是否使用公式
			if (StringUtils.isEmpty(row.calculateFormula)) {
				sb.append(")</td>");
				sb.append("<td><input type='text'  name='cost_" + cell.id
						+ "'  id='cost_" + cell.code
						+ "'  class='isnotcalculate' data-code='" + cell.code
						+ "'  data-calculate='" + row.calculateFormula
						+ "' value='" + (row.value == null ? "" : row.value)
						+ "'></td>");
			} else {
				sb.append("=");
				sb.append(row.calculateFormula);
				sb.append(")</td>");
				sb.append("<td><input type='text' name='cost_" + cell.id
						+ "'  id='cost_" + cell.code
						+ "' class='iscalculate'  data-code='" + cell.code
						+ "' readonly='readonly'  data-calculate='"
						+ row.calculateFormula + "' value='"
						+ (row.value == null ? "" : row.value) + "'></td>");
			}

			sb.append("</tr>");
		}

		return sb.toString();
	}

	@Override
	public String renderHtml(boolean ifShowProceReady) {
		// TODO Auto-generated method stub
		return renderHtml();
	}
}
